package com.sphd.restaurantsystem.userView;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;

import com.sphd.restaurantsystem.R;

import java.util.Calendar;

/**
 * Created by Hu on 2016/11/8.
 */

public class SrarchTime {
    Context context;
    DateListener dateListener;

    public void setDateListener(DateListener dateListener) {
        this.dateListener = dateListener;
    }

    public SrarchTime(Context context) {
        this.context = context;
    }

    public void showSearchTime() {
        //显示开始时间和结束时间的对话框
        LayoutInflater li = LayoutInflater.from(context);
        View promptsView = li.inflate(R.layout.layout_start_end_time, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        alertDialogBuilder.setView(promptsView);
        final DatePicker startDatePicker = (DatePicker) promptsView
                .findViewById(R.id.start_datePicker);
        final DatePicker endDatePicker = (DatePicker) promptsView
                .findViewById(R.id.end_datePicker);
        startDatePicker.init(startDatePicker.getYear(), 0, 1, (view, year, monthOfYear, dayOfMonth) -> {
            Calendar c = Calendar.getInstance();
            c.set(year, monthOfYear, dayOfMonth);
            endDatePicker.init(startDatePicker.getYear() + 1, startDatePicker.getMonth(),
                    startDatePicker.getDayOfMonth(), dateSetListener);
        });
        endDatePicker.init(startDatePicker.getYear() + 1, startDatePicker.getMonth(), startDatePicker.getDayOfMonth(), dateSetListener);

        alertDialogBuilder.setTitle("编辑查询时间");
        alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("确定",
                        (dialog, id) -> {
                            String startTimeSearch = startDatePicker.getYear() + "-" +
                                    ((startDatePicker.getMonth() + 1) < 10 ? "0" + (startDatePicker.getMonth() + 1) :
                                            (startDatePicker.getMonth() + 1))
                                    + "-" + ((startDatePicker.getDayOfMonth() + 1) < 10 ? "0" + startDatePicker.getDayOfMonth() :
                                    startDatePicker.getDayOfMonth());
                            String endTimeSearch = endDatePicker.getYear() + "-" +
                                    ((endDatePicker.getMonth() + 1) < 10 ? "0" + (endDatePicker.getMonth() + 1) :
                                            (endDatePicker.getMonth() + 1))
                                    + "-" + (endDatePicker.getDayOfMonth() < 10 ? "0" + endDatePicker.getDayOfMonth() :
                                    endDatePicker.getDayOfMonth());
                            dateListener.getDate(startTimeSearch, endTimeSearch);

                            dialog.dismiss();
                        })
                .setNegativeButton("取消",
                        (dialog2, id) -> dialog2.cancel());
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }


    private DatePicker.OnDateChangedListener dateSetListener = (view, year, monthOfYear, dayOfMonth) -> {
        Calendar c = Calendar.getInstance();
        c.set(year, monthOfYear, dayOfMonth);
        System.out.println("TEST");
    };

    public interface DateListener {
        void getDate(String startDate, String endDate);
    }
}
